package cn.cxq.learning.e2_1;

/**
 * 计算sin值
 */
public class SinUtil {

    private static double PI = 3.1415926535897932; //圆洲率

    public static double sinTNV(double x) {
        int k, n;
        double y = 1.0, xx;
        n = getSinItemNumber(x);
        k = n * 2;
        xx = x * x;
        while (k > 0) {
            y = 1.0 - y * xx / k / (k + 1);
            k -= 2;
        }
        return y * x;
    }

    private static int getSinItemNumber(double x) {
        int n;

        if (x < 0) x = -x;
        if (x <= PI / 8) n = 6;
        else if (x <= PI / 4) n = 8;
        else if (x <= PI / 2) n = 10;
        else if (x <= 3 * PI / 4) n = 12;
        else n = 14;

        return n;
    }
}
